package com.smart.stock.module.maindata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smart.stock.common.util.Result;
import com.smart.stock.common.util.db.DBUtil;
import com.smart.stock.common.util.h2.H2Util;
import com.smart.stock.common.util.spring.EntityUtil;
import com.smart.stock.module.maindata.entity.Stock;
import com.smart.stock.module.maindata.entity.StockPriceDaily;
import com.smart.stock.module.maindata.services.StockCodeInfoService;
import com.smart.stock.module.maindata.services.StockDailyIndexesService;
import com.smart.stock.module.maindata.services.StockDailyPriceServices;
import com.smart.stock.module.maindata.services.StockService;
import com.smart.stock.module.spider.jobSpiderApi.XueQiuApi;
import com.smart.stock.module.spider.util.TradingDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;


/**
 * @author chenguoxiang
 * @create 2018-11-02 15:49
 **/
@Api(tags = "股票信息")
@Slf4j
@Controller
@RequestMapping("/stockIndustry")
public class StockIndustryController {

    @Autowired
    private StockService service;

    @Autowired
    private StockCodeInfoService stockCodeInfoService;
    @Autowired
    private StockDailyPriceServices stockDailyPriceServices;
    @Autowired
    private TradingDateUtil tradingDateUtil;
    @Autowired
    private XueQiuApi xueQiuApi;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StockDailyIndexesService stockDailyIndexesService;


    @ApiOperation(value = "涨跌停分析", notes = "根据传入参数返回列表信息")
//    @ResponseBody
    @GetMapping("/index")
    public String index(Model model) {
        return "/stock/industry/industry";
    }



    @SneakyThrows
    @ApiOperation(value = "涨跌停Top分析", notes = "根据传入参数返回列表信息")
    @ResponseBody
    @GetMapping(value = "/getDataByDate")
    public Result getDataByDate(@RequestParam("tradeDate") Integer tradeDate) {
        String entityName = EntityUtil.getEntityNameByDate(StockPriceDaily.class, tradeDate.toString());
        if (!H2Util.tableExist(entityName)){
            List<StockPriceDaily> listByDate = H2Util.createListFromMongoByDate(template, StockPriceDaily.class, tradeDate);
            H2Util.createH2TableFromList(listByDate, entityName);
        }

        if (!H2Util.tableExist("STOCK")){
            H2Util.createH2TableFromMongo(template, Stock.class,null);
        }
        //todo 20210818,每天只有283条数据
        String sql = "SELECT b.industry,count(*) cnt FROM "+entityName+" a, STOCK  b  WHERE a.code=b.code and a.code not in ('SH000001','SZ399001','SZ399006','SH000688') and pctchg>9.5 group by industry order by cnt desc limit 5";
        log.info("getDataByDate_sql:"+sql);
        ResultSet rs = H2Util.execQuerySQL(sql);
        JSONArray json1 = DBUtil.resultSetToJson(rs);
//        ArrayList<List> list = new ArrayList<>();
//        ArrayList<String> list1 = new ArrayList<>();
//        ArrayList<String> list2 = new ArrayList<>();
//        while (rs.next()) {
//            list1.add(rs.getString("industry"));
//            list2.add(rs.getString("cnt"));
//        }

        sql = "SELECT b.industry,count(*) cnt FROM "+entityName+" a, STOCK  b  WHERE a.code=b.code and a.code not in ('SH000001','SZ399001','SZ399006','SH000688') and pctchg<-5 group by industry order by cnt desc limit 5";
        log.info("getDataByDate_sql:"+sql);
        rs = H2Util.execQuerySQL(sql);
        JSONArray json2 = DBUtil.resultSetToJson(rs);

//        ArrayList<String> list3 = new ArrayList<>();
//        ArrayList<String> list4 = new ArrayList<>();
//        while (rs.next()) {
//            list3.add(rs.getString("industry"));
//            list4.add(rs.getString("cnt"));
//        }

//        list.add(list1);
//        list.add(list2);
//        list.add(list3);
//        list.add(list4);
        //[[航空装备, 高低压设备, 船舶制造, 综合Ⅱ, 电源设备], [2, 2, 1, 1, 1], [金属非金属新材料, 计算机应用, 工业金属], [1, 1, 1]]

        System.out.println(json1);
        System.out.println(json2);
        JSONObject object = new JSONObject();
        object.put("json1",json1);
        object.put("json2",json2);
        Result<JSONObject> success = Result.success(object);
        return success;
    }




}
